Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

152
Visualizações
How to resolve error: "undefined" when reading single values from JSON using JavaScript?

I have a JavaScript function that outputs all values from a JSON file to a Terminal, but when attempting to print a single value, I get the following error: Line 74 - Cannot read properties of undefined (reading 'width')

Here is the code:

var json = `{
    "columns": [{
            "fname": "John",
            "lname": "Doe",
            "age": "23"
        },
        {
            "fname": "Jane",
            "lname": "Doe",
            "age": ""
        },
        {
            "fname": "Tom",
            "lname": "Johns",
            "age": ""
        },
        {
            "fname": "Brian",
            "lname": "Wicks",
            "age": "27"
        },
        {
            "fname": "Tim",
            "lname": "Stone",
            "age": ""
        },
        {
            "fname": "Fred",
            "lname": "Wilson",
            "age": ""
        },
        {
            "fname": "Jason",
            "lname": "Voorhees",
            "age": "90"
        }
    ],
    "rows": [{
        "data": {
            "width": "2",
            "height": "7",
            "length": "",
            "finalresult": "44",
            "firstresult": "",
            "secondresult": "300.00",
            "thirdresult": "700.40"
        }
    }]
}`;


var obj = JSON.parse(json);


function printValues(obj) {
    for(var k in obj) {
        if(obj[k] instanceof Object) {
            printValues(obj[k]);
        } else {
            console.log(obj[k] + "\n");
        };
    }
};

// Printing all the values from the resulting object
printValues(obj);

//print single value
//console.log(obj["columns"]["rows"]["data"]["width"] + "\n"); 
console.log("Print Indiviual values:");
console.log(obj["rows"]["data"]["width"] + "\n"); //The Error

...Could I get some assistance as to what I'm doing wrong? ...Thanks

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

As @Yarin and @codemonkey pointed out—you're trying to access the data object as a direct object property of rows, e.g. obj.rows.data which is wrong and will return undefined because data is not a property of the rows object. Instead it's the first (and only) entry in the array of objects that is rows.

You need to either change the syntax to:

obj.rows[0].data.width

Or take the data object out of the array and remove the array so that the rows object is structured like this:

const obj = {
  rows: {
    data: {
      width: 1
    }
  }
}

// Then you can use this syntax...
console.log( obj.rows.data.width )

// or even this syntax if you want to...
console.log( obj['rows']['data']['width'] )

about 4 years ago · Juan Pablo Isaza Relatório

0

I see that the width is the property of object data that is inside array rows...

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda